<?php
// +----------------------------------------------------------------------
// | OneKeyAdmin [ Believe that you can do better ]
// +----------------------------------------------------------------------
// | Copyright (c) 2020-2023 http://onekeyadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: MUKE <513038996@qq.com>
// +----------------------------------------------------------------------
namespace plugins\blog\api\model;

use think\Model;
use app\index\addons\Url;
use app\api\model\User;
use app\api\model\Config;
use app\api\model\Catalog;

class Blog extends Model
{
    // 设置json类型字段
    protected $json = ['field', 'images'];
    
    // 设置JSON数据返回数组
    protected $jsonAssoc = true;

    // 关联模型
    public function user()
    {
        return $this->hasOne(User::class, 'id', 'user_id')->append(['url', 'group']);
    }

    // 搜索器
    public function searchStatusAttr($query, $value, $array)
    {
        if ($value !== '' && $value !== null) {
            $query->where("status", '=', $value);
        }
    }

    public function searchKeywordAttr($query, $value, $array)
    {
        if (! empty($value)) {
            $query->where("title|c_title|description", 'like', '%' . $value . '%');
        }
    }

    public function searchDateAttr($query, $value, $array)
    {
        if (! empty($value)) { 
            $query->whereBetweenTime('create_time', $value[0], $value[1]);
        }
    }
    
    public function searchAttrAttr($query, $value, $array)
    {
        if (! empty($value)) {
            $query->where($value, '=', 1);
        }
    }

    public function searchCatalogIdAttr($query, $value, $array)
    {
        if (! empty($value)) {
            $query->where('catalog_id', 'find in set', $value);
        }
    }

    public function searchUserIdAttr($query, $value, $array)
    {
        if (! empty($value)) {
            $query->where('user_id', '=', $value);
        }
    }

    // 获取器
    public function getIsCollectAttr($value, $array)
    {
        $where[] = ['user_id', '=', request()->userId];
        $where[] = ['blog_id', '=', $array['id']];
        return BlogCollect::where($where)->count() > 0;
    }
    
    public function getFriendTimeAttr($value, $array)
    {
        return friend_time($array['create_time']);
    }

    public function getUrlAttr($value, $array)
    {
        if (!empty($array['id'])) {
            return Url::single($array);
        }
    }

    public function getCommentCountAttr($value, $array)
    {
        return BlogComment::where('blog_id', $array['id'])->count();
    }
    
    public function getCollectCountAttr($value, $array)
    {
        return BlogCollect::where('blog_id', $array['id'])->count();
    }

    public function getCatalogIdAttr($value, $array)
    {
        return $value ? array_map('intval', explode(',', $value)) : [];
    }
    
    public function getGroupIdAttr($value, $array)
    {
        return empty($value) ? [] : explode(',', $value);
    }

    public function getFieldAttr($value, $array)
    {
        $field = [];
        $config = Config::getVal('app_blog');
        if (isset($config['field'])) {
            foreach ($config['field'] as $k => $v) {
                switch ($v['type']['is']) {
                    case 'el-link-select':
                        $field[$v['field']] = isset($value[$v['field']]) ? Url::appoint($value[$v['field']]) : '';
                        break;
                    case 'el-array':
                        $arr = isset($value[$v['field']]) ? $value[$v['field']]['table'] : [];
                        foreach ($arr as $key1 => $val1) {
                            foreach (array_keys($val1) as $key2 => $val2) {
                                if (is_array($val1[$val2])) {
                                    $arr[$key1][$val2] = Url::appoint($val1[$val2]);
                                }
                            }
                        }
                        $field[$v['field']] = $arr;
                        break;
                    default:
                        $field[$v['field']] = isset($value[$v['field']]) ? $value[$v['field']] : '';
                        break;
                }
            }
        }
        return $field;
    }

    public function getContentImagesAttr($value, $array) {
        $images = [];
        if (! empty($array['content'])) {
            $reg = "/(src)=(\\\?)([\"|']?)([^ \"'>]+\.(gif|jpg|jpeg|bmp|png))\\2\\3/i";
            preg_match_all($reg, $array['content'], $match);
            if (! empty($match[0])) {
                foreach ($match[0] as $key => $img) {
                    preg_match('/"(.*?)"/', $img, $src);
                    if (! empty($src)) {
                        $images[] = $src[1];
                    }
                }
            }
            return $images;
        }
    }

    // 修改器
    public function setFieldAttr($value, $array)
    {
        $field = [];
        foreach ($value as $key => $val) {
            $field[$val['field']] = $val['type']['value'];
        }
        return $field;
    }

    public function setGroupIdAttr($value, $array)
    {
        return implode(',', $value);
    }
    
    public function setCatalogIdAttr($value, $array)
    {
        return implode(',', $value);
    }

    public function setRelationAttr($value, $array)
    {
        return implode(',', $value);
    }

    public function setDescriptionAttr($value, $array)
    {
        return empty($value) ? csubstr(input('content')) : $value;
    }

    public function setSeoDescriptionAttr($value, $array)
    {
        return empty($value) ? csubstr(input('content')) : $value;
    }
}